<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-col :span="24">
      <el-form v-loading="loading" :model="dataForm" :rules="dataRule" ref="dataForm" :label-width="$i18n.locale === 'en-US' ? '120px' : '100px'">
        <el-col :span="24">
          <!-- 模板名称 -->
          <el-form-item label="模板名称" prop="tmpName">
            <!--文本框-->
            <el-input v-model="dataForm.tmpName" placeholder="模板名称" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 模板编码 -->
          <el-form-item label="模板编码" prop="tmpCode">
            <!--文本框-->
            <el-input v-model="dataForm.tmpCode" placeholder="模板编码" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 模板分类 -->
          <el-form-item label="模板分类" prop="tmpType">
            <el-select v-model="dataForm.tmpType" placeholder="模板分类" clearable>
              <el-option v-for="template in $getDictList('GEN_TEMP_TYPE', true)"
                         :key="template.val"
                         :label="template.label"
                         :value="template.val"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 模板预览图片 -->
          <el-form-item label="模板预览图片">
            <!--文本框-->
            <uni-upload-pro ref="upload" :uploadOriProps="uploadOriProps" v-model="pictureList">
            </uni-upload-pro>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 排序 -->
          <el-form-item label="排序" prop="sorts">
            <!--计数器-->
            <el-input-number v-model="dataForm.sorts" :min="1" :max="50"></el-input-number>
          </el-form-item>
        </el-col>
      </el-form>
    </el-col>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" :loading="loading" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>
<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      loading: false,
      visible: false,
      dataForm: {
        id: '',
        tmpCode: '',
        tmpName: '',
        tmpType: '',
        picture: '',
        sorts: '',
        remark: ''
      },
      pictureList: [],
      uploadOriProps: {
        drag: true,
        multiple: false,
        'list-type': 'picture-card',
        limit: 1,
        data: {
          dirValue: 'HT01',
          fileClass: 'image'
        }
      }
    }
  },
  computed: {
    dataRule () {
      return {
        tmpCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        tmpName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        tmpType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        sorts: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.pictureList = []
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.loading = true
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/gen/genTableType/${this.dataForm.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return false
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
        if (this.dataForm.picture && this.dataForm.picture !== '') {
          this.pictureList.push(
            {
              name: this.dataForm.tmpName,
              url: this.dataForm.picture
            }
          )
        }
      }).catch(() => { })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.loading = true
        if (this.pictureList.length > 0) {
          this.dataForm.picture = this.pictureList[0].url
        } else {
          this.dataForm.picture = ''
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/gen/genTableType/', this.dataForm).then(({ data: res }) => {
          this.loading = false
          if (res.code !== 0) {
            return false
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
